package com.kehutong.wwin.event.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.coraframework.inject.Inject;
import org.coraframework.inject.Singleton;
import org.coraframework.json.JSONObject;
import org.coraframework.util.CallBack;
import org.coraframework.util.Objects;

import com.kehutong.common.DoveClient;
import com.kehutong.common.tencent.AesException;
import com.kehutong.common.tencent.WXBizMsgCrypt;

@Singleton
public final class DecEncryptUtil {
	
	private DecEncryptUtil() {
		configMap = new HashMap<>();
	}
	
	@Inject
	private DoveClient doveClient;
	// 此处不用同步
	private Map<String, CorpConfig> configMap;
	
	public String decEncrypt(String corpId, String type, String content, HttpServletRequest req) throws Exception {
		CorpConfig config = getConfig(corpId);
		 return decEncrypt(config, type, content, req);
    }
	
	public String decEncrypt(String corpId, String type, String content, HttpServletRequest req, CallBack<CorpConfig> called) throws Exception {
		CorpConfig config = getConfig(corpId);
		called.call(config);
		
        return decEncrypt(config, type, content, req);
    }
	
	private String decEncrypt(CorpConfig config, String type, String content, HttpServletRequest req) throws AesException {
        WXBizMsgCrypt wxcpt;
		if ("1".equals(type)) {
			wxcpt = new WXBizMsgCrypt(config.getEmployeeToken(), config.getEmployeeEncodingAESKey(), config.getCorpId());
		} else if ("2".equals(type)) {
			wxcpt = new WXBizMsgCrypt(config.getContactToken(), config.getContactEncodingAESKey(), config.getCorpId());
		} else {
			return "";
		}
        
        //解析出url上的参数值如下：
		String msg_signature = req.getParameter("msg_signature");
        String timestamp = req.getParameter("timestamp");
        String nonce = req.getParameter("nonce");
        String echostr = content.replace(' ', '+');

        // 需要返回的明文
        return wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr);
	}

	public CorpConfig getConfig(String corpId) throws Exception {
		CorpConfig config = configMap.get(corpId);
		if (Objects.nonNull(config)) {
			return config;
			
		}
		JSONObject rs = doveClient.post(HttpAPI.DECENCRYPT_CONFIG, http->{
			http.addParam("corpId", corpId);
		});
		
		config = new CorpConfig(corpId, rs.getJSONObject("data"));
		configMap.put(corpId, config);
		return config;
	}
	
	public class CorpConfig {
		
		private CorpConfig(String corpId, JSONObject config) {
			this.corpId = corpId;
			this.khtId = config.getString("khtId");
			this.employeeToken = config.getString("employeeToken");
			this.employeeEncodingAESKey = config.getString("employeeEncodingAESKey");
			this.contactToken = config.getString("contactToken");
			this.contactEncodingAESKey = config.getString("contactEncodingAESKey");
		}
		
		private final String corpId;
		private final String khtId;
		private final String employeeToken;
		private final String employeeEncodingAESKey;
		private final String contactToken;
		private final String contactEncodingAESKey;
		
		public String getCorpId() {
			return corpId;
		}
		
		public String getKhtId() {
			return khtId;
		}

		public String getEmployeeToken() {
			return employeeToken;
		}
		
		public String getEmployeeEncodingAESKey() {
			return employeeEncodingAESKey;
		}
		
		public String getContactToken() {
			return contactToken;
		}
		
		public String getContactEncodingAESKey() {
			return contactEncodingAESKey;
		}
	}

}
